package com.tarena.lbs.marketing.consumer;

import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.marketing.event.CouponInput;
import com.tarena.lbs.marketing.service.CouponService;
import com.tarena.lbs.pojo.marketing.param.UserReceiveCouponParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.stereotype.Component;
@Component
@Slf4j
@EnableBinding(CouponInput.class)
public class CouponConsumer {
    @Autowired
    private CouponService couponService;
    @StreamListener("user-get-coupon-input")
    public void handleUserGetCoupon(UserReceiveCouponParam param){
        //领取优惠券就是给用户领取记录 新增一条数据 谁 领了 哪个优惠券 的哪个code码
        //消费方法 异常处理 业务异常 只记录不抛出 系统异常抛出
        try {
            couponService.saveUserReceiveCoupon(param);
        }catch (BusinessException e){
            log.error("领取优惠券 失败 业务异常",e);
        }catch (Throwable e){
            log.error("领取优惠券 失败 系统异常",e);
            throw e;
        }
    }
}
